import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
        },
    },
    // 配置CSS，解决 -ms-high-contrast 弃用警告
    css: {
        // 开发服务器相关的配置
        devSourcemap: true,
        // 配置CSS预处理和后处理
        postcss: {
            plugins: [
                // 忽略 -ms-high-contrast 相关的警告
                {
                    postcssPlugin: 'ignore-ms-high-contrast',
                    Once(root) {
                        // 遍历所有CSS规则
                        root.walkAtRules(/^media$/, (rule) => {
                            // 查找包含 -ms-high-contrast 的媒体查询
                            if (rule.params && rule.params.includes('-ms-high-contrast')) {
                                // 将这些规则标记为已处理，防止产生警告
                                rule.nodes = rule.nodes || [];
                            }
                        });
                    },
                },
            ],
        },
    },
    server: {
        port: 3000,
        open: true,
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:19900',
                changeOrigin: true,
                secure: false,
            },
            '/img-api': {
                target: 'http://127.0.0.1:19900',
                changeOrigin: true,
                secure: false,
                rewrite: (path) => path.replace(/^\/img-api/, ''),
            },
            '/audio-transcribe': {
                target: 'http://127.0.0.1:9977',
                changeOrigin: true,
                secure: false,
                rewrite: (path) => path.replace(/^\/audio-transcribe/, ''),
            },
        },
    },
    build: {
        outDir: 'dist',
        sourcemap: true,
        rollupOptions: {
            output: {
                manualChunks: {
                    vendor: ['react', 'react-dom'],
                    antd: ['antd', '@ant-design/icons'],
                    utils: ['axios', '@tanstack/react-query'],
                },
            },
        },
    },
});
